package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.List;


@Service
@ApiOperation("套餐接口")
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public PageResult pageSelectSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        //这里使用第三方工具进行分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //调用mapper层去查询数据
        List<SetmealVO> setmeals = setmealMapper.pageSelectSetmeal(setmealPageQueryDTO);
        Page page = (Page) setmeals;
        return new PageResult(page.getTotal(),page.getResult());
    }


    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        //新增的套餐默认为停售状态
        setmealDTO.setStatus(StatusConstant.DISABLE);
        Setmeal setMeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setMeal);
        //添加菜品
        setmealMapper.addSetmeal(setMeal);

        //获取套餐里的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        System.out.println(setmealDishes);
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setMeal.getId());
            setmealDishMapper.insert(setmealDish);
        });
    }

    /**
     * 根据ID查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealDTO selectByIdSetmeal(Integer id) {
        //根据id查询套餐属性内容
        SetmealDTO setmealDTO = setmealMapper.selectByIdSetmeal(id);
        //根据id查询菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.seleteDish(id);
        setmealDTO.setSetmealDishes(setmealDishes);
        return setmealDTO;
    }




    /**
     * 删除套餐
     * @param ids
     */
    @Override
    public void deleteSetmeal(List<Long> ids) {
        //删除  在售状态下，不可删除套餐
        //判断套餐是否在开售
        ids.forEach(id -> {
            SetmealDTO setmealDTO = setmealMapper.selectByIdSetmeal(id.intValue());
            if (setmealDTO.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //正常删除
        setmealMapper.deleteSetmeal(ids);
    }

    /**
     * 修改状态
     * @param status
     * @param id
     */
    @Override
    public void EnableDisable(Integer status, Integer id) {
        //status：状态   id：当前套餐ID
        //套餐内包含未启售菜品，无法启售
        Setmeal stmt = new Setmeal();
        stmt.setStatus(status);
        stmt.setId(id.longValue());
        //根据套餐id查询
        List<SetmealDish> setmealDishes = setmealDishMapper.seleteDish(id);
        //进行健壮性判断
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(dish -> {
                Dish dish1 = dishMapper.selectByIdDish(dish.getDishId().intValue());
                if (dish1.getStatus() == StatusConstant.DISABLE) {
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
            //修改状态
            setmealMapper.EnableDisable(stmt);
        }
    }

    /**
     * 修改套餐
     * @param setmealVO
     */
    @Override
    public void updateSetmeal(SetmealVO setmealVO) {
        //修改套餐，因为我们公共字段已经用AOP赋过值了
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmealMapper.EnableDisable(setmeal);
        //获取修改的套餐ID
        Long id = setmeal.getId();
        //如果修改的有菜品,我们获取菜品信息进行修改
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        if (setmealDishes != null) {
            //先根据ID删除原有的菜品
            setmealDishMapper.deleteByIdSetmealDish(id);
            //遍历菜品重新添加
            setmealDishes.forEach(setmealDish -> {
                //遍历菜品重新添加到菜品表里
                setmealDish.setSetmealId(id);
                //调用mapper层重新添加
                setmealDishMapper.insert(setmealDish);
            });
        }
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
